import 'package:flutter/material.dart';

class GSYTabBarWidget extends StatefulWidget {
  static const int BOOTOM_TAB = 1;

  static const int TOP_TAB = 2;

  final int type;
  final bool resizeToAvoidBottomPadding;
  final List<Widget> tabItems;
  final List<Widget> tabViews;
  final Color backgroundColor;
  final Color indicatorColor;
  final Widget title;
  final Widget drawer;
  final Widget floatingActionButton;
  final FloatingActionButtonLocation floatingActionButtonLocation;
  final Widget bottomBar;
  final TarWidgetControl tarWidgetControl;
  final ValueChanged<int> onPageChanged;

  const GSYTabBarWidget(
      {Key key,
      this.type,
      this.resizeToAvoidBottomPadding,
      this.tabItems,
      this.tabViews,
      this.backgroundColor,
      this.indicatorColor,
      this.title,
      this.drawer,
      this.floatingActionButton,
      this.floatingActionButtonLocation,
      this.bottomBar,
      this.tarWidgetControl,
      this.onPageChanged})
      : super(key: key);

  @override
  _GSYTabBarWidgetState createState() => _GSYTabBarWidgetState(
        type,
        tabViews,
        indicatorColor,
        drawer,
        floatingActionButton,
        tarWidgetControl,
        onPageChanged,
      );
}

class _GSYTabBarWidgetState extends State<GSYTabBarWidget>
    with SingleTickerProviderStateMixin {
  final int _type;
  final List<Widget> _tabViews;
  final Color _indicatorColor;
  final Widget _drawer;
  final Widget _floatingActionButton;
  final TarWidgetControl _tarWidgetControl;
  final PageController _pageController = PageController();
  final ValueChanged<int> _onPageChanged;

  _GSYTabBarWidgetState(
      this._type,
      this._tabViews,
      this._indicatorColor,
      this._drawer,
      this._floatingActionButton,
      this._tarWidgetControl,
      this._onPageChanged)
      : super();

  TabController _tabController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController =
        new TabController(length: widget.tabItems.length, vsync: this);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (this._type == GSYTabBarWidget.BOOTOM_TAB) {
      return new Scaffold(
          drawer: _drawer,
          appBar: new AppBar(
            backgroundColor: Theme.of(context).primaryColor,
            title: widget.title,
          ),
          body: new PageView(
            controller: _pageController,
            children: _tabViews,
            onPageChanged: (index) {
              _tabController.animateTo(index);
              _onPageChanged?.call(index);
            },
          ),
          bottomNavigationBar: new Material(
            color: Theme.of(context).primaryColor,
            child: new SafeArea(
              child: new TabBar(
//                labelColor: Colors.blueGrey,
                controller: _tabController,
                tabs: widget.tabItems,
                indicatorColor: _indicatorColor,
                onTap: (index) {
                  _onPageChanged?.call(index);
                  _pageController
                      .jumpTo(MediaQuery.of(context).size.width * index);
                },
              ),
            ),
          ));
    }

    return new Scaffold(
      drawer: _drawer,
      floatingActionButton: _floatingActionButton,
      appBar: new AppBar(
        backgroundColor: widget.backgroundColor,
        title: widget.title,
        bottom: new TabBar(
          controller: _tabController,
          tabs: widget.tabItems,
          indicatorColor: _indicatorColor,
          onTap: (index) {
            _onPageChanged?.call(index);
            _pageController.jumpTo(MediaQuery.of(context).size.width * index);
          },
        ),
      ),
      body: new PageView(
        controller: _pageController,
        children: _tabViews,
        onPageChanged: (index) {
          _onPageChanged?.call(index);
          _tabController.animateTo(index);
        },
      ),
      bottomNavigationBar: widget.bottomBar,
    );
  }
}

class TarWidgetControl {
  List<Widget> footerButton = [];
}
